package com.qianfeng.xinyihezi.xinyihezi.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.qianfeng.xinyihezi.xinyihezi.R;
import com.qianfeng.xinyihezi.xinyihezi.adapter.FragmentAdapter;
import com.qianfeng.xinyihezi.xinyihezi.config.MyNetConfig;
import com.qianfeng.xinyihezi.xinyihezi.config.MyRetrofit;
import com.qianfeng.xinyihezi.xinyihezi.fragment.ZhuTiFragment;
import com.qianfeng.xinyihezi.xinyihezi.model.ZhuTiBean;
import com.qianfeng.xinyihezi.xinyihezi.utils.GlideImageLoader;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerClickListener;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * 作者：王加威 on 2016/11/14 14:03
 */

public class ZhuTiGuanActivity extends AppCompatActivity {

    private String mGo_id;
    private String mObj_name;
    private TextView mTextView;
    private Banner mBanner;
    private ImageView man,woman,iv1,iv2,iv3,iv4,iv5,iv6;
    private ZhuTiBean bean;
    private int mPage;
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private FragmentAdapter mFragmentAdapter;
    private String[] titles;
    private List<Fragment>mFragments;
    private TextView back;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zhutiguan_activity);
        back= (TextView) findViewById(R.id.back);
        mTextView= (TextView) findViewById(R.id.zhutiguan_title);
        mBanner = (Banner) findViewById(R.id.zhutiguan_topView);
        mTabLayout= (TabLayout) findViewById(R.id.zhutiguan_tablayout);
        mViewPager= (ViewPager) findViewById(R.id.zhutiguan_ViewPager);
        iv1= (ImageView) findViewById(R.id.zhutiguan_liwu_iv1);
        iv2= (ImageView) findViewById(R.id.zhutiguan_liwu_iv2);
        iv3= (ImageView) findViewById(R.id.zhutiguan_liwu_iv3);
        iv4= (ImageView) findViewById(R.id.zhutiguan_liwu_iv4);
        iv5= (ImageView) findViewById(R.id.zhutiguan_liwu_iv5);
        iv6= (ImageView) findViewById(R.id.zhutiguan_liwu_iv6);
        man= (ImageView) findViewById(R.id.zhutiguan_man);
        woman= (ImageView) findViewById(R.id.zhutiguan_woman);
        Intent intent=getIntent();
        mGo_id = intent.getStringExtra("go_id");
        mObj_name = intent.getStringExtra("obj_name");
        Log.e("name",mObj_name);
        Log.e("name",mGo_id);
        mTextView.setText(mObj_name);
        initData(Integer.valueOf(mGo_id));
        setListener();
    }

    private void setListener() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initData(int page) {
        Retrofit retrofit=new Retrofit.Builder().baseUrl(MyNetConfig.BASE_PATH).build();
        MyRetrofit myRetrofit=retrofit.create(MyRetrofit.class);
        myRetrofit.getZhuTiData(page).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response.isSuccessful()) {
                    try {
                         bean =  new Gson().fromJson(response.body().string(),ZhuTiBean.class);
                            setPic(bean);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }


            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Toast.makeText(ZhuTiGuanActivity.this, "失败", Toast.LENGTH_SHORT).show();
            }
        });
    }
    private void setPic(final ZhuTiBean bean) {
        Glide.with(this).load(bean.data.snd_banner.get(0).image_url).into(man);
        Glide.with(this).load(bean.data.snd_banner.get(1).image_url).into(woman);
        Glide.with(this).load(bean.data.goods.get(0).image_url).into(iv1);
        Glide.with(this).load(bean.data.goods.get(2).image_url).into(iv2);
        Glide.with(this).load(bean.data.goods.get(1).image_url).into(iv3);
        Glide.with(this).load(bean.data.goods.get(3).image_url).into(iv4);
        Glide.with(this).load(bean.data.goods.get(1).image_url).into(iv5);
        Glide.with(this).load(bean.data.goods.get(4).image_url).into(iv6);
        List<String> url=new ArrayList<>();

        for (int i = 0; i < bean.data.banner.size(); i++) {
            url.add(bean.data.banner.get(i).image_url);
        }
        mBanner.setImages(url);
        mBanner.setImageLoader(new GlideImageLoader());
        mBanner.setDelayTime(1500).isAutoPlay(true).setIndicatorGravity(BannerConfig.CENTER)
                .setOnBannerClickListener(new OnBannerClickListener() {
                    @Override
                    public void OnBannerClick(int position) {
                        Intent intent=new Intent(ZhuTiGuanActivity.this,ShowActivity.class);
                        intent.putExtra("url",bean.data.banner.get(position).name);
                        startActivity(intent);
                    }
                });
        mBanner.start();

        mFragments=new ArrayList<>();
        titles=new String[bean.data.gift_cat_id.size()];
        for (int i = 0; i < titles.length; i++) {
            titles[i]=bean.data.gift_cat_id.get(i).name;
            Bundle bundle=new Bundle();
            ZhuTiFragment fragment=new ZhuTiFragment();
            bundle.putString("title",titles[i]);
            bundle.putString("gods_id",bean.data.gift_cat_id.get(i).gos_id);
            fragment.setArguments(bundle);
            mFragments.add(fragment);
        }
        mFragmentAdapter=new FragmentAdapter(getSupportFragmentManager(),mFragments,titles);
        mViewPager.setAdapter(mFragmentAdapter);
        mTabLayout.setupWithViewPager(mViewPager);
    }

}
